//
//  SendSubjectViewController.m
//  xiaoyiNew
//
//  Created by TW on 2017/2/7.
//  Copyright © 2017年 讯一. All rights reserved.
//

#import "SendSubjectViewController.h"
#import "SubjectsViewController.h"
#import "SendSubjectImagesView.h"

@interface SendSubjectViewController ()<SubjectsViewControllerDelegate, SendSubjectImagesViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *subjectBut;//主题
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *subjectWidth;//主题宽度

@property (weak, nonatomic) IBOutlet UITextView *contentTV;
@property (weak, nonatomic) IBOutlet UILabel *defaultTextLabel;//默认文字
@property (weak, nonatomic) IBOutlet UIButton *AddImageButton;
@property (weak, nonatomic) IBOutlet SendSubjectImagesView *ImagesView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *ImagesHeight;

@end

@implementation SendSubjectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"发布主题";
    self.navigationController.navigationBar.tintColor = UIColorFromHex(0x01c7a6);

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(action_sendClick)];
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"叉叉"] style:UIBarButtonItemStylePlain target:self action:@selector(action_balckClick)];
    //初始化
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_textViewChange:) name:UITextViewTextDidChangeNotification object:nil];
    [self addWidth:1 color:JGRGBAColor(220, 220, 220, 1) radius:12 sender:self.subjectBut];
    [self addWidth:2 color:JGRGBAColor(196, 196, 196, 1) radius:10 sender:self.AddImageButton];
    CGFloat width = [self calculateTextWidth:self.subjectBut.titleLabel.text] + 50;
    self.subjectWidth.constant = width;
  
    self.ImagesView.delegate = self;
}
//给控件添加边框,切圆角.宽度 边框颜色 切圆弧度
- (void)addWidth:(CGFloat)width color:(UIColor *)color radius:(CGFloat)radius sender:(UIButton *)sender{
    sender.layer.borderWidth = width;
    sender.layer.borderColor = color.CGColor;
    sender.layer.cornerRadius = radius;
    sender.layer.masksToBounds = YES;
}

#pragma mark - SendSubjectImagesViewDelegate -
- (void)pushToPhotoPage:(UIImagePickerController *)page {

        [self showViewController:page sender:nil];
}
- (void)changeHeightWithIndex:(NSInteger)index {

    if (index > 3 && index < 8) {
        
        self.ImagesHeight.constant = (Screen_Width - 60)/4 * 2 + 30;
    }else if (index >= 8) {
    
        self.ImagesHeight.constant = (Screen_Width - 60)/4 * 3 + 50;
    }else {
    
        self.ImagesHeight.constant = 128;
    }
}
#pragma mark - SubjectsViewControllerDelegate -
//选择主题
- (void)subjectsChangeWithStr:(NSString *)str {

    [self.subjectBut setTitle:str forState:0];
    CGFloat width = [self calculateTextWidth:str] + 50;
    self.subjectWidth.constant = width;
}

#pragma mark - action -
//计算宽度
- (CGFloat)calculateTextWidth:(NSString *)str {
    
    CGSize titleSize = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
    return titleSize.width;
}
#pragma mark - 点击 -
//返回
- (void)action_balckClick {

    [self.navigationController popViewControllerAnimated:YES];
}
//选择主题
- (IBAction)action_chooseSubjectClick:(UIButton *)sender {
    
    SubjectsViewController * subjectVC = [[SubjectsViewController alloc] init];
    subjectVC.delegate = self;
    self.navigationController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:subjectVC animated:YES];
}
//选择图片
- (IBAction)changeImage:(UIButton *)sender {
    
    
    
}
//@提醒
- (IBAction)action_remindClick:(id)sender {
    
}
//发送
- (void)action_sendClick {

    
}


#pragma mark - 通知 -
- (void)action_textViewChange:(NSNotification *)sender {

    if (self.contentTV.text.length > 0) {
        self.defaultTextLabel.hidden = YES;
    }else {
        
        self.defaultTextLabel.hidden = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
